/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hanuma;

import java.util.*;

/**
 *
 * @author niyaz
 */
public class ParameterizedUserList {

    List<ParameterizedUser> users = new LinkedList<ParameterizedUser>();

    public ParameterizedUserList(UserList userlist, List<Filter> filters) throws Exception {
        VKontakteApi vk = new VKontakteApi(Conf.token, Conf.id);
        for (String user : userlist.list) {
            ParameterizedUser buf = new ParameterizedUser();
            buf.user = user;
            // Date start = new Date();
            // System.out.println(start);
            Person person = vk.getPerson(user);

            //  System.out.println(new Date().getTime() - start.getTime());
            //  start = new Date();
            for (Filter filter : filters) {
                buf.parameters.add(filter.relevance(person));
            }
            //System.out.println(new Date().getTime() - start.getTime());
            users.add(buf);
            // System.out.println(buf.user+" "+buf.parameters.get(0));
        }
    }

    public ParameterizedUserList(String file, List<Filter> filters) throws Exception {
        FileStoredPersonReader reader = new FileStoredPersonReader(file);
        Iterator it = reader.getIterator();
        while (it.hasNext()) {
            Person person = (Person) it.next();
            ParameterizedUser buf = new ParameterizedUser();
            buf.user = person.ID;
            for (Filter filter : filters) {
                buf.parameters.add(filter.relevance(person));
            }
            users.add(buf);

            //log

            System.out.print(buf.user + " ");
            for (Double d : buf.parameters) {
                System.out.print(d + " ");
            }
            System.out.println();

        }
    }

    public List<ParameterizedUser> getBestN(int n, List<Double> factors) throws Exception {
        for (ParameterizedUser user : users) {
            if (factors.size() != user.parameters.size()) {
                throw new Exception("number of factors != namber of parameters");
            }
            user.score = 0;
            int it = 0;
            for (Double d : user.parameters) {
                user.score += d * factors.get(it);
                it++;
            }
        }
        Collections.sort(users);

        List<ParameterizedUser> out = new LinkedList<ParameterizedUser>();
        int count = 0;
        for (ParameterizedUser user : users) {
            if (count > n) {
                break;
            }
            out.add(user);
            count++;
        }
        return out;
    }
}
